package com.sz.virtualbank.web.controller.system;

import com.sz.virtualbank.common.annotation.LoginRequired;
import com.sz.virtualbank.common.result.CommonResponse;
import com.sz.virtualbank.dao.entity.SystemParam;
import com.sz.virtualbank.service.system.SystemParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统参数相关接口
 * 
 * @author Claude
 */
@RestController
@RequestMapping("/api/system")
@Slf4j
@Api(tags = "系统参数接口")
public class SystemController {

    @Autowired
    private SystemParamService systemParamService;
    
    /**
     * 获取系统参数
     */
    @GetMapping("/param/{paramKey}")
    @ApiOperation("获取系统参数")
    @LoginRequired
    public CommonResponse<String> getParamValue(@PathVariable String paramKey) {
        String paramValue = systemParamService.getParamValue(paramKey);
        return CommonResponse.success(paramValue);
    }
    
    /**
     * 获取所有系统参数
     */
    @GetMapping("/param/list")
    @ApiOperation("获取所有系统参数")
    @LoginRequired
    public CommonResponse<List<SystemParam>> getAllSystemParams() {
        List<SystemParam> params = systemParamService.getAllSystemParams();
        return CommonResponse.success(params);
    }
    
    /**
     * 设置系统参数（管理员接口）
     */
    @PostMapping("/param")
    @ApiOperation("设置系统参数")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paramKey", value = "参数键", dataType = "String", paramType = "query", required = true),
        @ApiImplicitParam(name = "paramValue", value = "参数值", dataType = "String", paramType = "query", required = true),
        @ApiImplicitParam(name = "paramDesc", value = "参数描述", dataType = "String", paramType = "query")
    })
    @LoginRequired
    public CommonResponse<Boolean> setSystemParam(
            @RequestParam String paramKey,
            @RequestParam String paramValue,
            @RequestParam(required = false) String paramDesc) {
        
        // TODO: 实际应用中需要进行管理员权限校验
        
        boolean result = systemParamService.setSystemParam(paramKey, paramValue, paramDesc);
        return CommonResponse.success(result);
    }
    
    /**
     * 更新系统参数（管理员接口）
     */
    @PutMapping("/param/{paramKey}")
    @ApiOperation("更新系统参数")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paramKey", value = "参数键", dataType = "String", paramType = "path", required = true),
        @ApiImplicitParam(name = "paramValue", value = "参数值", dataType = "String", paramType = "query", required = true)
    })
    @LoginRequired
    public CommonResponse<Boolean> updateSystemParam(
            @PathVariable String paramKey,
            @RequestParam String paramValue) {
        
        // TODO: 实际应用中需要进行管理员权限校验
        
        boolean result = systemParamService.updateSystemParam(paramKey, paramValue);
        return CommonResponse.success(result);
    }
    
    /**
     * 删除系统参数（管理员接口）
     */
    @DeleteMapping("/param/{paramKey}")
    @ApiOperation("删除系统参数")
    @LoginRequired
    public CommonResponse<Boolean> deleteSystemParam(@PathVariable String paramKey) {
        // TODO: 实际应用中需要进行管理员权限校验
        
        boolean result = systemParamService.deleteSystemParam(paramKey);
        return CommonResponse.success(result);
    }
}
